package com.hsj.demo.controller;

import com.hsj.demo.entity.LostInfo;
import com.hsj.demo.entity.User;
import com.hsj.demo.service.LostInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/lostInfo")
public class LostInfoController {
    @Autowired
    private LostInfoService lostInfoService;

    @RequestMapping("/findAll")
    public List<LostInfo> findAll(){
        return lostInfoService.findAll();
    }

    @RequestMapping("/findByUserId")
    public List<LostInfo> findByUserId(HttpSession session) {
        User user = (User)session.getAttribute("user");
        return lostInfoService.findByUserId(user.getId());
    }

    /**
     * 查询所有未审核的失物招领
     * @param lostInfo
     * @return
     */
    @RequestMapping("/findAuditLost")
    public List<LostInfo> findAuditLost(LostInfo lostInfo){
        return lostInfoService.findByEntity(lostInfo);
    }

    /**
     * 根据条件查询所有未审核的失物招领
     * @param lostInfo
     * @return
     */
    @RequestMapping("/findByEntity")
    public List<LostInfo> findByEntity(@RequestBody LostInfo lostInfo){
        return lostInfoService.findByEntity(lostInfo);
    }

    @RequestMapping("/findByKeyword")
    public List<LostInfo> findByKeyword(String keyword){
        return lostInfoService.findByKeyword(keyword);
    }

    @RequestMapping("/insert")
    public String insert(MultipartFile file, LostInfo lostInfo, HttpSession session){
        lostInfo.setUser((User) session.getAttribute("user"));
        return lostInfoService.insert(file,lostInfo);
    }

    /**
     * 修改失物招领审核状态
     * @param session
     * @param lostInfo
     * @return
     */
    @RequestMapping("/updateAudit")
    public String updateAudit(HttpSession session,LostInfo lostInfo){
        User user = (User)session.getAttribute("user");
        if(user.getRole()==2){
            lostInfoService.update(lostInfo);
            return "修改成功";
        }else{
            return "修改失败";
        }
    }

    /**
     * 用户修改自己的失物招领信息
     * @param file
     * @param lostInfo
     * @param session
     * @return
     */
    @RequestMapping("/update")
    public String update(MultipartFile file, LostInfo lostInfo, HttpSession session){
        lostInfo.setUser((User) session.getAttribute("user"));
        return lostInfoService.update(file,lostInfo);
    }

    @RequestMapping("/deleteById")
    public String deleteById(Integer id){
        lostInfoService.deleteById(id);
        return "删除成功";
    }

}
